//
//  StoreHotelDetailRequest.m
//  BaseProject
//
//  Created by 王洋 on 2021/1/25.
//  Copyright © 2021 王洋. All rights reserved.
//

#import "StoreHotelDetailRequest.h"

@implementation StoreHotelDetailRequest

+ (void)request_storeDetailWith:(NSString *)sellerId
                       comption:(void (^)(BOOL success, StoreHotelDetailModel *model))comption {
    
    StoreHotelDetailModel *hotelModel = [[StoreHotelDetailModel alloc] init];
    
    NSMutableDictionary *dic = @{}.mutableCopy;
    [dic setValue:sellerId forKey:@"bhSellerId"];
    [dic setValue:[TTUserModel sharedObject].userId forKey:@"userId"];

    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_HotelInfo];
    
    [SVProgressHUD showWithStatus:@"正在加载"];

    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *sysCouponList = [resultDic valueForKey:@"sysCouponList"];
                
                NSArray *sellerBanquetHallList = [resultDic valueForKey:@"sellerBanquetHallList"];
                NSArray *sellerBanquetHallMenuList = [resultDic valueForKey:@"sellerBanquetHallMenuList"];
                NSArray *sysWorryFreeLabel = [resultDic valueForKey:@"sysWorryFreeLabel"];
                
                NSArray *sellerOpusList = [resultDic valueForKey:@"sellerOpusList"];
                NSArray *userEvaluateList = [resultDic valueForKey:@"userEvaluateList"];
                NSArray *sellerOpusClassificationList = [resultDic valueForKey:@"sellerOpusClassificationList"];
                NSDictionary *seller = [resultDic valueForKey:@"seller"];
                NSDictionary *sellerDynamic = [resultDic valueForKey:@"sellerDynamic"];
                NSDictionary *userCollection = [resultDic valueForKey:@"userCollection"];

                hotelModel.sellerDynamic = [CommonDynamicModel modelWithDictionary:sellerDynamic];
                hotelModel.userCollection = [CommonFavorModel modelWithDictionary:userCollection];
                if (hotelModel.userCollection.isDel == 2) {
                    hotelModel.isCollect = YES;
                } else {
                    hotelModel.isCollect = NO;
                }
                
                NSArray *SellerPromiseLabelAuthernticationList = [resultDic valueForKey:@"SellerPromiseLabelAuthernticationList"];
                hotelModel.SellerPromiseLabelAuthernticationList = @[].mutableCopy;
                if ([SellerPromiseLabelAuthernticationList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < SellerPromiseLabelAuthernticationList.count; i++) {
                        NSDictionary *SellerPromiseLabelAuthernticationListDic = SellerPromiseLabelAuthernticationList[i];
                        CommonPromiseTagModel *model = [CommonPromiseTagModel modelWithDictionary:SellerPromiseLabelAuthernticationListDic];
                        [hotelModel.SellerPromiseLabelAuthernticationList addObject:model];
                    }
                }
                
                
                if ([sellerDynamic isKindOfClass:[NSDictionary class]]) {
                    NSArray *imageUrls = [sellerDynamic valueForKey:@"imageUrls"];
                    if ([imageUrls isKindOfClass:[NSArray class]]) {
                        hotelModel.sellerDynamic.imageUrls = @[].mutableCopy;
                        for (int i = 0; i < imageUrls.count; i++) {
                            NSDictionary *imgDic = imageUrls[i];
                            CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imgDic];
                            
                            if ([imgModel.type isEqualToString:@"dtimg"]) {
                                [hotelModel.sellerDynamic.imageUrls addObject:imgModel];
                            }
                        }
                    }
                }
                
                hotelModel.seller = [CommonSellerModule modelWithDictionary:seller];
                if (hotelModel.seller.lat > 0 && hotelModel.seller.lng > 0) {
                    hotelModel.seller.distance = [Utilities calculateStart:CLLocationCoordinate2DMake([TTUserModel sharedObject].latitude, [TTUserModel sharedObject].longitude) end:CLLocationCoordinate2DMake(hotelModel.seller.lat,hotelModel.seller.lng)];
                }
                
                
                NSArray *coverPaths = [seller valueForKey:@"coverPaths"];
                hotelModel.seller.coverPaths = @[].mutableCopy;
                if ([coverPaths isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < coverPaths.count; i++) {
                        CommonImageModel *model = [CommonImageModel modelWithDictionary:coverPaths[i]];
                        [hotelModel.seller.coverPaths addObject:model];
                    }
                }
                
                hotelModel.sysCouponList = @[].mutableCopy;
                if ([sysCouponList isKindOfClass:[NSArray class]]) {
                    for (int i=0; i < sysCouponList.count; i++) {
                        NSDictionary *sysCouponListDic = sysCouponList[i];
                        CommonCouponModel *couponModel = [CommonCouponModel modelWithDictionary:sysCouponListDic];
                        [hotelModel.sysCouponList addObject:couponModel];
                    }
                }
//
                hotelModel.userEvaluateList = @[].mutableCopy;
                if ([userEvaluateList isKindOfClass:[NSArray class]]) {
                    
                    for (int i=0; i < userEvaluateList.count; i++) {
                        NSDictionary *userEvaluateListDic = userEvaluateList[i];
                        
                        NSDictionary *sellerGoods = [userEvaluateListDic valueForKey:@"sellerGoods"];
                        StoreDetailEvaluateModel *evaluateModel = [StoreDetailEvaluateModel modelWithDictionary:userEvaluateListDic];
                        
                        NSArray *imageUrls = [userEvaluateListDic valueForKey:@"imageUrls"];
                        if ([imageUrls isKindOfClass:[NSArray class]]) {
                            
                            evaluateModel.imageUrls = @[].mutableCopy;
                            for (int j = 0; j < imageUrls.count; j++) {
                                NSDictionary *imgDic = imageUrls[j];
                                CommonImageModel *imgModel = [CommonImageModel modelWithDictionary:imgDic];
                                [evaluateModel.imageUrls addObject:imgModel];
                            }
                        }
                        
                        evaluateModel.infoHeight = 60.0f;
                        evaluateModel.evaluateTextHeight = [Utilities getStringHeightWithText:evaluateModel.ueEvaluateContent font:[UIFont systemFontOfSize:12] viewWidth:TTScreenWidth-30];
                        NSInteger lineCount = evaluateModel.imageUrls.count/3;
                        if (evaluateModel.imageUrls.count%3>0) {
                            lineCount++;
                        }
                        evaluateModel.collectionViewHeight = lineCount * ((TTScreenWidth - 30)/3);
                        evaluateModel.thumbHeight = 40.0f;
                        evaluateModel.packageHeight = CGFLOAT_MIN;

                        evaluateModel.itemHeight = evaluateModel.infoHeight + evaluateModel.evaluateTextHeight + evaluateModel.collectionViewHeight + evaluateModel.thumbHeight;
                        
                        evaluateModel.sellerGoods = [CommonPackageModel modelWithJSON:sellerGoods];
                        [hotelModel.userEvaluateList addObject:evaluateModel];
                    }
                }
                
                hotelModel.sellerBanquetHallList = @[].mutableCopy;
                if ([sellerBanquetHallList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerBanquetHallList.count; i++) {
                        NSDictionary *sellerBanquetHallListDic = sellerBanquetHallList[i];
                        CommonHallModel *packageModel = [CommonHallModel modelWithDictionary:sellerBanquetHallListDic];
                        [hotelModel.sellerBanquetHallList addObject:packageModel];
                    }
                }
                
                hotelModel.sellerBanquetHallMenuList = @[].mutableCopy;
                if ([sellerBanquetHallMenuList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerBanquetHallMenuList.count; i++) {
                        NSDictionary *sellerBanquetHallMenuListDic = sellerBanquetHallMenuList[i];
                        CommonMenuModel *menuModel = [CommonMenuModel modelWithJSON:sellerBanquetHallMenuListDic];
                        menuModel.bhmMenuCount = 1;
                        menuModel.dishesList = @[].mutableCopy;
                        NSArray *f_dishesList = [sellerBanquetHallMenuListDic valueForKey:@"dishesList"];
                        if ([f_dishesList isKindOfClass:[NSArray class]]) {
                            for (NSDictionary *f_disDic in f_dishesList) {
                                V2CommonMenuModel *f_model = [V2CommonMenuModel modelWithJSON:f_disDic];
                                f_model.dishesList = @[].mutableCopy;
                                if ([f_disDic isKindOfClass:[NSDictionary class]]) {
                                    NSArray *s_dishesList = [f_disDic valueForKey:@"dishesList"];
                                    if ([s_dishesList isKindOfClass:[NSArray class]]) {
                                        for (NSDictionary *s_disDic in s_dishesList) {
                                            V2CommonMenuModel *s_model = [V2CommonMenuModel modelWithJSON:s_disDic];
                                            [f_model.dishesList addObject:s_model];
                                        }
                                    }
                                    [menuModel.dishesList addObject:f_model];
                                }
                            }
                        }
                        [hotelModel.sellerBanquetHallMenuList addObject:menuModel];
                    }
                }
                
                hotelModel.sellerOpusList = @[].mutableCopy;
                if ([sellerOpusList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerOpusList.count; i++) {
                        NSDictionary *sellerOpusListDic = sellerOpusList[i];
                        CommonOpusModel *model = [CommonOpusModel modelWithDictionary:sellerOpusListDic];
                        [hotelModel.sellerOpusList addObject:model];
                    }
                }
                
                hotelModel.sellerOpusClassificationList = @[].mutableCopy;
                if ([sellerOpusClassificationList isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sellerOpusClassificationList.count; i++) {
                        NSDictionary *sellerOpusClassificationListDic = sellerOpusClassificationList[i];
                        CommonOpusCatModel *model = [CommonOpusCatModel modelWithDictionary:sellerOpusClassificationListDic];
                        if (i==0) {model.isSelect = YES;}
                        else {model.isSelect = NO;}
                        model.itemWidth = [Utilities getStringWidthWithString:model.opusClassName fontSize:12 andHeight:30] + 20;
                        [hotelModel.sellerOpusClassificationList addObject:model];
                    }
                }
                
                hotelModel.sysWorryFreeLabel = @[].mutableCopy;
                if ([sysWorryFreeLabel isKindOfClass:[NSArray class]]) {
                    for (int i = 0; i < sysWorryFreeLabel.count; i++) {
                        NSDictionary *sysWorryFreeLabelDic = sysWorryFreeLabel[i];
                        CommonWYXTagModel *model = [CommonWYXTagModel modelWithDictionary:sysWorryFreeLabelDic];
                        [hotelModel.sysWorryFreeLabel addObject:model];
                    }
                }
                
                if (sysCouponList.count == 0) {
                    hotelModel.couponHeight = CGFLOAT_MIN;
                } else {
                    hotelModel.couponHeight = CGFLOAT_MIN;
                }
                
                if (hotelModel.seller.freeService.length > 0) {
                    hotelModel.serviceHeight = 60;
                } else {
                    hotelModel.serviceHeight = CGFLOAT_MIN;
                }
                
                if (hotelModel.seller.sellerNotice.length > 0) {
                    hotelModel.gonggaoHeight = 60;
                } else {
                    hotelModel.gonggaoHeight = CGFLOAT_MIN;
                }
                
                if (hotelModel.seller.consultingStar.length > 0) {
                    hotelModel.zixunHeight = 60;
                } else {
                    hotelModel.zixunHeight = CGFLOAT_MIN;
                }

                if (hotelModel.seller.arrivalGift.length > 0) {
                    hotelModel.daodianHeight = 60;
                } else {
                    hotelModel.daodianHeight = CGFLOAT_MIN;
                }
                

                comption(YES,hotelModel);
            } else {
                
                comption(NO,nil);
                [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
                [SVProgressHUD dismissWithDelay:1];
            }
            
            
        } else {
            
            comption(NO,nil);
            [SVProgressHUD showErrorWithStatus:[data valueForKey:@"message"]];
            [SVProgressHUD dismissWithDelay:1];
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        [SVProgressHUD dismiss];
        comption(NO,nil);

    }];
    
    
    
}

@end
